package com.dly.hope.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.*;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.schema.ModelSpecification;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

/**
 * @author linyu.dai
 * @project myserve
 * @description swagger配置类
 * @creat 2022/8/26 09:46:56
 */
@Configuration
@EnableSwagger2 //该注解是Springfox-swagger框架提供的使用Swagger注解，该注解必须加
@EnableKnife4j  //该注解是knife4j提供的增强注解,Ui提供了例如动态参数、参数过滤、接口排序等增强功能,如果你想使用这些增强功能就必须加该注解，否则可以不用加
public class SwaggerConfig {


    @Value("${jwt.header}")
    private String jwtHeader;

    @Value("${swagger.path}")
    private String pathMapping;

    /**
     * 接口构建器
     *
     * @return
     */
    @Bean
    public Docket docket() {

        RequestParameterBuilder jwtTokenPar = new RequestParameterBuilder();
        List<RequestParameter> pars = Lists.newArrayList();

        //添加一个全局参数
        jwtTokenPar.name(jwtHeader)
                .description("JWT token")
                .query(parameterSpecificationBuilder -> parameterSpecificationBuilder.defaultValue("")
                        .allowEmptyValue(true))
                .in(ParameterType.HEADER);

        pars.add(jwtTokenPar.build());

        return new Docket(DocumentationType.OAS_30)
                .globalRequestParameters(pars)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.dly.hope.controller"))
                .paths(PathSelectors.any())
                .build()
                .groupName("common模块")
                .enable(true)
                //所有的url 加上前缀api
                .pathMapping(pathMapping);
    }

    /**
     * Api页面信息配置
     *
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("swagger文档")
                .description("swagger文档")
                .version("V1.0")
                .build();
    }
}
